package com.cskaoyan.config4;

import com.alibaba.druid.pool.DruidDataSource;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * @author stone
 * @date 2022/06/24 15:13
 */
@Configuration
@EnableConfigurationProperties(CskaoyanDatasourceProperties.class)
// 该注解可以把其value属性对应的类注册为容器中的组件
public class DataSourceConfiguration4 {

    // 把成员变量提取到参数类中之后，我们想要获得之前的值，就需要通过参数类的实例来获得
/*
    @Autowired
    CskaoyanDatasourceProperties properties;
*/
    CskaoyanDatasourceProperties properties;

    public DataSourceConfiguration4(CskaoyanDatasourceProperties properties) {
        this.properties = properties;
    }

    @Bean
    public DataSource dataSource4() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(properties.getDriverClass());
        dataSource.setUrl(properties.getUrl());
        dataSource.setUsername(properties.getUsername());
        dataSource.setPassword(properties.getPassword());
        return dataSource;
    }
}
